#include "tools/queue_trian.h"
#include "headers.h"
#include <string.h>


static int capicity=0;
static SQItem *arr=NULL;
static int size=0;
static int head=0;
static int fail=0;

// 初始化队列的容量:
int init_queue_h(int _capicity){
    capicity=_capicity;
    arr=calloc(capicity,sizeof(SQItem));
    if(arr)
        return 0;
    capicity=0;
    return -1;
}

// 入队value,成功0,失败-1
int enqueue_h(const SQItem *value){
    if(size<capicity)
    {
        size++;
        memcpy(&arr[fail],value,sizeof(SQItem));
        fail=(fail+1)%capicity;
        return 0;
    }
    return -1;
}

// 出队首元素,返回值-1失败,成功0
int dequeue_h(SQItem *result){
    if(size==0)
        return -1;
    memcpy(result,&arr[head],sizeof(SQItem));
	memset(&arr[head],0,sizeof(SQItem));
    head=(head+1)%capicity;
    size--;
    return 0;
}

// 返回当前元素的数量
int getqueunsize_h(){
    return size;
}



void destoryqueun_h(){
    if(arr)
        free(arr);
    arr=NULL;
}
